package net.jeeeyul;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Say extends AbstractChatServlet {
   private static final long serialVersionUID = 415570406935949678L;

   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {

      String chatURL = (String) req.getSession().getAttribute("url");
      String message = req.getParameter("message");
      String sender = req.getParameter("sender");
      if (sender == null) {
         sender = "무명씨";
      }

      PersistenceManager manager = PMF.get().getPersistenceManager();

      System.out.println(chatURL + " : " + message);

      ChatRoom chatRoom = ChatRoom.get(manager, chatURL);
      chatRoom.addClient(getClientId(req));
      chatRoom.sendMessage(sender, message);

      manager.makePersistent(chatRoom);
      manager.close();
   }
}
